package day10.gec.threadlocal4;

public class ThreadLocalDemo {
    public static void main(String[] args) {

        // 创建ThreadLocal对象
        final ThreadLocal<String> local = new ThreadLocal<>();

        new Thread() {
            @Override
            public void run() {
                // 设置值
                local.set("天王盖地虎");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 在当前线程内访问 threadlocal的值
                String str = local.get();
                System.out.println("str:" + str);

                // 只管调用
                // local.remove();
                // System.out.println("str:" + str);
            }
        }.start();

        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ", 访问 local: " + local.get());
        // 不行 main线程
    }
}
